﻿using Lufei_CaterModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Lufei_CaterDAL
{
    public class R_OrderInfo_Product_DAL
    {

        /// <summary>
        /// 查询所有的订单信息
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public List<R_OrderInfo_Product> GetOrderInfoProducts(int orderID)
        {
            string sql = "select ROrderProId,proName,proPrice,UnitCount,ProUnit,catname,BeginTime from productinfo inner join r_orderinfo_product on productinfo.proid=r_orderinfo_product.proid inner join categoryinfo on categoryinfo.catid= productinfo.catid inner join orderinfo on r_orderinfo_product.orderid=orderinfo.orderid where productinfo.delflag=0 and orderinfo.delflag=0 and orderinfo.orderstate=1 and categoryinfo.delflag=0 and r_orderinfo_product.delflag=0 and  orderinfo.orderid=" + orderID;

            DataTable dt=  SqlHelper.ExecuteDataTable(sql);
            List<R_OrderInfo_Product> list = new List<R_OrderInfo_Product>();
            if (dt.Rows.Count>0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    //关系转模型
                    list.Add(RowsToR_OrderInfo_Product(dr));
                }
            }
            return list;
        }

        //string sql="select * from r_orderingo_product"
        //关系 -----》模型   sql语句-----》类模型
        //转型转模型 是类对应sql
        public R_OrderInfo_Product RowsToR_OrderInfo_Product(DataRow dr)
        {
            R_OrderInfo_Product r = new R_OrderInfo_Product();
            r.ROrderProId = Convert.ToInt32(dr["ROrderProId"]);
            r.ProName = dr["proName"].ToString();
            r.ProPrice = Convert.ToDecimal(dr["proPrice"]);
            r.UnitCount = Convert.ToDecimal(dr["UnitCount"]);
            r.ProUnit = dr["ProUnit"].ToString();
            r.CatName = dr["catname"].ToString();
            r.SubTime = Convert.ToDateTime(dr["BeginTime"]);
            //金额=单价*数量
            r.ProMoney = r.ProPrice * r.UnitCount;
            return r;
        }




        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int InsertDate(R_OrderInfo_Product model)
        {
            string sql = "insert into r_orderinfo_product(orderid,proid,delflag,subtime,state,unitcount)values(@orderid,@proid,@delflag,@subtime,@state,@unitcount)";
            SQLiteParameter[] oaram = new SQLiteParameter[]
            {
                new SQLiteParameter("@orderid",model.OrderId),
                new SQLiteParameter("@proid",model.ProId),
                new SQLiteParameter("@delflag",model.DelFlag),
                new SQLiteParameter("@subtime",model.SubTime),
                new SQLiteParameter("@state",model.State),
                new SQLiteParameter("@unitcount",model.UnitCount),
            };
           return SqlHelper.ExecuteNonQuery(sql, oaram);
        }



        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelteInfoByID(int id)
        {
            string sql = "update R_OrderInfo_Product set DelFlag=1 where ROrderProId="+id;

            return   SqlHelper.ExecuteNonQuery(sql);
        }

    }
}
